class BookViewModel:
    def __init__(self, ys_book):
        self.title = ys_book['title']
        self.publisher = ys_book['publisher']
        self.author = '、'.join(ys_book['author'])
        self.image = ys_book['image']
        self.price = ys_book['price']
        self.summary = ys_book['summary']
        self.isbn = ys_book['isbn']
        self.pages = ys_book['pages']
        self.pubdate = ys_book['pubdate']
        self.binding = ys_book['binding']

    @property
    def intro(self):
        intros = filter(lambda x: True if x else False, [
            self.author, self.publisher, self.price
        ])
        return '/'.join(intros)


class BookCollection:
    def __init__(self):
        self.total = 0
        self.books = []
        self.keyword = ''

    def fill(self, ys_book, keyword):
        self.total = ys_book.total
        self.books = [BookViewModel(book) for book in ys_book.books]
        self.keyword = keyword


class _BookViewModel:

    @classmethod
    def __cut_book_data(cls, data):
        return {
            'title': data['title'],
            'publisher': data['publisher'],
            'author': ','.join(data['author']),
            'pages': data['pages'] or '',
            'price': data['price'],
            'summary': data['summary'] or '',
            'image': data['image'],
        }

    @classmethod
    def package_single(cls, data, keyword):
        return {
            'books': [cls.__cut_book_data(data)],
            'total': 1 if data else 0,
            'keyword': keyword
        }

    @classmethod
    def package_collection(cls, data, keyword):
        return {
            'books': [cls.__cut_book_data(book) for book in data['books']],
            'total': data['total'],
            'keyword': keyword
        }
